package com.gzhryc.net_marketing.cloud.notice;

import com.gzhryc.common.api.DefaultAPIResponse;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.core.notice.goods.GoodsAuditResultNotice;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;

/**
 * 供货商订单通知管理
 * @version 2023-10-10
 */
public class SupplyChainNoticeService {

    static Logger log = Logger.getLogger(SupplyChainNoticeService.class);

    private static SupplyChainNoticeService me;

    public static SupplyChainNoticeService self(){
        if(me == null){
            me = new SupplyChainNoticeService();
        }
        return me;
    }

    private String httpDomain;
    private CloseableHttpClient httpClient;

    public void initial(String httpDomain) {
        this.httpDomain = httpDomain;
        this.httpClient = HttpClients.createDefault();
    }

    public DefaultAPIResponse goodsAuditResultNotice(Long supplyId,GoodsAuditResultNotice apiReq){
        CloseableHttpResponse response = null;
        try {
        	///market/supplier/notice/{supplierId}/goods/audit/result
            URI uri = new URIBuilder(httpDomain + "/market/supplier/notice/"+supplyId+"/goods/audit/result").build();

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("商品审核结果通知返回：" + str);
                if(StringUtils.isNotBlank(str)) {
                    DefaultAPIResponse apiResp = JsonTools.fromJson(str, DefaultAPIResponse.class);
                    return apiResp;
                }
            }
        } catch(Exception e){
            log.error("商品审核结果通知错误，" + e.getMessage(), e);
        } finally {
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    
    
}
